package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.bean.AuthListBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

/**
 * 授权管理页面
 */
public class AuthorizationActivity extends BaseActivity implements View.OnClickListener, InitializeView {
    private TextView tv_pay_authorization = null;
    private TextView tv_repayment_authorization = null;
    private RequestPresenter mPresenter = null;
    private String zwgt_data;
    private boolean ALREADY_AUTHORIZATION;
    private boolean ALREADY_PAY_AUTHORIZATION;
    private int repayment_max_amt;
    private int repayment_min_amt;
    private int repayment_max_deadline;
    private int repayment_min_deadline;

    private int payment_max_amt;
    private int payment_min_amt;
    private int payment_max_deadline;
    private int payment_min_deadline;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_authrization_manage;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        toolbar_title.setText(getString(R.string.authorization_manage));
        mPresenter = new InitializePresenterImpl(mContext, this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadingData();
    }

    private void loadingData() {
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        //授权情况
        mPresenter.loadingData(TaskConstants.TASK_GETAUTHLIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_GETAUTHLIST, Constants.EVENT_REFRESH_DATA,
                BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.TASK_GETAUTHLIST, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.
                        getShareProf(UserInfo.TOKENID), zwgt_data), true);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    public void findView() {
        tv_pay_authorization = findView(R.id.tv_pay_authorization);
        tv_repayment_authorization = findView(R.id.tv_repayment_authorization);
        tv_pay_authorization.setOnClickListener(this);
        tv_repayment_authorization.setOnClickListener(this);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //缴费授权
            case R.id.tv_pay_authorization:
                if (ALREADY_PAY_AUTHORIZATION) {
                    Bundle bundle = new Bundle();
                    bundle.putString("AUTHORIZATION", Extras.PAYMENT_AUTHORIZATION);
                    readyGoForResult(AuthorizationDetailActivity.class,0,bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt(Extras.PAYMENT_MAX_AMT, payment_max_amt);
                    bundle.putInt(Extras.PAYMENT_MIN_AMT, payment_min_amt);
                    bundle.putInt(Extras.PAYMENT_MAX_DEADLINE, payment_max_deadline);
                    bundle.putInt(Extras.PAYMENT_MIN_DEADLINE, payment_min_deadline);
                    readyGoForResult(PayAuthorizationActivity.class, 1,bundle);
                }
                break;
            case R.id.tv_repayment_authorization:
                if (ALREADY_AUTHORIZATION) {
                    Bundle bundle = new Bundle();
                    bundle.putString("AUTHORIZATION", Extras.REPAYMENT_AUTHORIZATION);
                    readyGoForResult(AuthorizationDetailActivity.class,0,bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt(Extras.REPAYMENT_MAX_AMT, repayment_max_amt);
                    bundle.putInt(Extras.REPAYMENT_MIN_AMT, repayment_min_amt);
                    bundle.putInt(Extras.REPAYMENT_MAX_DEADLINE, repayment_max_deadline);
                    bundle.putInt(Extras.REPAYMENT_MIN_DEADLINE, repayment_min_deadline);
                    readyGoForResult(RepaymentAuthorizationActivity.class, 2,bundle);
                }
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_GETAUTHLIST:
                AuthListBean authListBean = (AuthListBean) data;
                repayment_max_amt = authListBean.repay_auth.rule.max_amt.max;
                repayment_min_amt = authListBean.repay_auth.rule.max_amt.min;
                repayment_max_deadline = authListBean.repay_auth.rule.deadline.max;
                repayment_min_deadline = authListBean.repay_auth.rule.deadline.min;

                payment_max_amt = authListBean.payment_auth.rule.max_amt.max;
                payment_min_amt = authListBean.payment_auth.rule.max_amt.min;
                payment_max_deadline = authListBean.payment_auth.rule.deadline.max;
                payment_min_deadline = authListBean.payment_auth.rule.deadline.min;

                if (authListBean.repay_auth.rst == 0) {
                    ALREADY_AUTHORIZATION = false;
                } else {
                    ALREADY_AUTHORIZATION = true;
                }

                if (authListBean.payment_auth.rst == 0) {
                    ALREADY_PAY_AUTHORIZATION = false;
                } else {
                    ALREADY_PAY_AUTHORIZATION = true;
                }

                tv_pay_authorization.setEnabled(true);
                tv_repayment_authorization.setEnabled(true);
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_GETAUTHLIST:

                break;
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch (resultCode) {
//            case CANCEL_AUTHORIZATION_SUCCESS:
//                ALREADY_AUTHORIZATION = false;
//                break;
//            case AUTHORIZATION_SUCCESS:
//
//                ALREADY_AUTHORIZATION = true;
//                break;
//        }
//    }
}
